<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-form :inline="true" :model="dataForm">
          <el-form-item label="分类">
            <category-cascader :categoryIds.sync="categoryIds"></category-cascader>
          </el-form-item>
          <el-form-item label="品牌">
            <el-select style="width:160px" placeholder="请选择" v-model="dataForm.brandId" filterable clearable>
              <el-option v-for="item in brands" :key="item.brandId" :label="item.brandName" :value="item.brandId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select style="width:160px" v-model="dataForm.status" clearable>
              <el-option label="新建" :value="0"></el-option>
              <el-option label="上架" :value="1"></el-option>
              <el-option label="下架" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="检索">
            <el-input style="width:160px" v-model="dataForm.key" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchSpuInfo()">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="24">
        <spuinfo ref="spuinfo" :dataForm="dataForm"></spuinfo>
      </el-col>
    </el-row>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import CategoryCascader from "../common/category-cascader";
import Spuinfo from "./spuinfo";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: { CategoryCascader, Spuinfo },
  props: {},
  data() {
    //这里存放数据
    return {
      categoryIds: [],
      dataForm: {
        status: "",
        key: "",
        brandId: "",
        categoryId: "",
      },
      brands: [],
      //catPathSub: null,
      //brandIdSub: null,
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    categoryIds(path) {
      //监听到路径变化需要查出这个三级分类的分组信息
      console.log("路径变了", path);
      this.dataForm.categoryId = path[path.length - 1];
      if (path && path.length == 3) {
        console.log("方法正在运行");
        this.$http({
          url: this.$http.adornUrl(
            `/product/categorybrand/brands/list/${this.dataForm.categoryId}`
          ),
          method: "get",
          params: this.$http.adornParams({ page: 1, limit: 10000000 }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.brands = data.data;
            console.log(this.brands);
          } else {
            this.$message.error(data.msg);
          }
        });
      } else if (path.length == 0) {
        this.dataForm.categoryId = "";
      } else {
        this.dataForm.categoryId = "";
      }
    },
  },
  //方法集合
  methods: {
    searchSpuInfo() {
      console.log("搜索条件", this.dataForm);
      //TODO 条件查询 此处存在问题
        this.$refs.spuinfo.getDataList();
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // this.catPathSub = PubSub.subscribe("categoryIds", (msg, val) => {
    //   this.dataForm.categoryId = val[val.length-1];
    // });
    // this.brandIdSub = PubSub.subscribe("brandId", (msg, val) => {
    //   this.dataForm.brandId = val;
    // });
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {
    //  PubSub.unsubscribe(this.catPathSub);
    //  PubSub.unsubscribe(this.brandIdSub);
  }, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped>
</style>